import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: '/login'
  // },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue') // 路由懒加载

  },
  {
    path: '/reg',
    component: () => import('../views/reg/index.vue')
  },
  {
    path: '/',
    component: () => import('../views/Layout/index.vue'),
    children: [
      { path: '', component: () => import('../views/Home/index.vue') },
      { path: 'artcategory', component: () => import('../views/art/artcategory.vue') },
      { path: 'artlist', component: () => import('../views/art/artlist.vue') },
      { path: 'changeavatar', component: () => import('../views/user/changeavatar.vue') },
      { path: 'resetpwd', component: () => import('../views/user/resetpwd.vue') },
      { path: 'userinfo', component: () => import('../views/user/userinfo.vue') }
    ]
  },
  {
    path: '*',
    component: () => import('../views/NoFound.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to and from are Route Object,next() must be called to resolve the hook
  var whitelist = ['/login', '/reg']
  if (whitelist.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})
export default router
